package com.ruoyi.mk.service.impl;

import java.io.File;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Stream;

import com.ruoyi.common.config.RuoYiConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mk.mapper.MkTariffMapper;
import com.ruoyi.mk.domain.MkTariff;
import com.ruoyi.mk.service.IMkTariffService;

/**
 * mk_tariffService业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-02
 */
@Service
public class MkTariffServiceImpl implements IMkTariffService 
{
    @Autowired
    private MkTariffMapper mkTariffMapper;

    private ExecutorService executor = Executors.newFixedThreadPool(10);


    /**
     * 查询mk_tariff
     * 
     * @param id mk_tariff主键
     * @return mk_tariff
     */
    @Override
    public MkTariff selectMkTariffById(Long id)
    {
        return mkTariffMapper.selectMkTariffById(id);
    }

    /**
     * 查询mk_tariff列表
     * 
     * @param mkTariff mk_tariff
     * @return mk_tariff
     */
    @Override
    public List<MkTariff> selectMkTariffList(MkTariff mkTariff)
    {
        return mkTariffMapper.selectMkTariffList(mkTariff);
    }


    /**
     * 新增mk_tariff
     *
     * @param mkTariff mk_tariff
     * @return
     */
    @Override
    public int insertMkTariff(MkTariff mkTariff) {
//        executor.submit(() -> {
            return mkTariffMapper.insertMkTariff(mkTariff);
//        });
//        return -1;
    }
    public void shutdown() {
        executor.shutdown();
    }

    /**
     * 修改mk_tariff
     * 
     * @param mkTariff mk_tariff
     * @return 结果
     */
    @Override
    public int updateMkTariff(MkTariff mkTariff)
    {
        return mkTariffMapper.updateMkTariff(mkTariff);
    }

    /**
     * 批量删除mk_tariff
     * 
     * @param ids 需要删除的mk_tariff主键
     * @return 结果
     */
    @Override
    public int deleteMkTariffByIds(Long[] ids)
    {
        Stream.of(ids).forEach(id -> {
            String path = mkTariffMapper.selectMkTariffById(id).getPicture();
            imageremoveList(path);
        });
        return mkTariffMapper.deleteMkTariffByIds(ids);
    }

    /**
     * 删除mk_tariff信息
     * 
     * @param id mk_tariff主键
     * @return 结果
     */
    @Override
    public int deleteMkTariffById(Long id)
    {
        return mkTariffMapper.deleteMkTariffById(id);
    }

    @Override
    public boolean imageremove(String path) {
        int startIndex = path.indexOf("uploadPath") + "uploadPath".length();
        String start = path.substring(startIndex);
         String result = start.substring(0,start.length() -2);
        String localPath = RuoYiConfig.getProfile() + result;
        File file = new File(localPath);
        if (file.exists()) {
            if (file.delete()) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public boolean imageremoveList(String path) {
        int startIndex = path.indexOf("uploadPath") + "uploadPath".length();
        String result = path.substring(startIndex);
//        String result = start.substring(0,start.length() -2);
        String localPath = RuoYiConfig.getProfile() + result;
        File file = new File(localPath);
        if (file.exists()) {
            if (file.delete()) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}
